


%num_of_blocks=8;

num_of_blocks=16;
%get the participant number
partnum=input('What is the participant number?'); 
plot_behav=input('would you like to plot behavioral data? (Y or N)');
%plot_behav=input('would you like to plot behavioral data? (Y or N)', 's'); %changed by DOS 12/23/2020

%get participant num in character form/way it would appear in a filename
    %(with a zero in front for <10) in order to accurately retrieve file
if partnum < 10
    participant_num=sprintf('0%d',partnum);
else
    participant_num=sprintf('%d',partnum);
end
for block=1:num_of_blocks
 %for block=8           
    filenames{block}=sprintf('/autofs/space/clive_001/users/olivia/GE_new/MEG/GE_%s/events/GE_%s_B%d.eve'...
        ,participant_num,participant_num,block);
   trigref_filenames{block}=sprintf('/autofs/space/clive_001/users/olivia/GE_new/MEG/GE_matlab_triggers/GE_B%d.txt',block);
    %load events for block x
    og_events=load(filenames{block});
    [soundfile_names,reftrigs]=textread(trigref_filenames{block},'%s\t%d');
    error_trigs=reftrigs(find(reftrigs>300),:);
    error_number=1;
    %copy row that has all zeros for first row
    first_row=NaN(1,4);
    for u=1:length(og_events)
        if og_events(u,3)==0 & og_events(u,4)==0
            first_row=og_events(u,:);
            break
        end
    end
    %remove events indicating button release or trigger turning off
    og_events=og_events(find(~og_events(:,4)==0),:);
    %add in first row
    og_events=vertcat(first_row,og_events);
    %initialize new event array that will only contain events in which trigger onset was succeeded by a single response
   new_events=nan(size(og_events,1),size(og_events,2)); 
   behav_events=nan(length(og_events),1);
   
   %retrieve all events and their corresponding response (the first resp. bc subject may have held finger on button)
   for eventnum=1:length(og_events)
   %for eventnum=1:length(og_events)-1 %Changed by DOS 12/23/2020
       
        %leave the first row of event file as 0s across per convention
        if eventnum==1 
            new_events(eventnum,:)=og_events(eventnum,:);
            
         %if the event is a trigger and its followed by a response, put the trigger and response into new event file  
        elseif eventnum>1 & og_events(eventnum,4)<1000 & og_events(eventnum+1,4)>1000
            event=og_events(eventnum,4); %retrieve the particular event youre "recoding"
            newevent=0; %set the new event code to 0 so you can add to it depending on variables
            response=og_events(eventnum+1,4); %retrieve the subject's response to the event in question
            % figure out whether subject responded with 'word' or 'nonword'
                %responses: 1024=3=green=word; 2048=4=red=nonword
                %responses: 16384=3=green=word; 4294934528=4=red=nonword

        %%%%%%%%    %%%%%%%%%%%%%%%%
            if event==255
                event=error_trigs(error_number,1);
                error_number=error_number+1;
            end
            if event>300 & response==2048 %correctly responded nonword to nonword stim; 3000
            %if event>300 & response==4294934528 %correctly responded nonword to nonword stim; 3000

                newevent=event+3000;
            elseif event>300 & response==1024 %incorrectly responded word to nonword stim; 4000
            %elseif event>300 & response==16384 %incorrectly responded word to nonword stim; 4000
                newevent=event+4000;
            elseif event<300 & response==1024 %correctly responded word to word stim; 1000
            %elseif event<300 & response==16384 %correctly responded word to word stim; 1000

                newevent=event+1000;
            elseif event<300 & response==2048 %incorrectly responded nonword to word stim;2000
            %elseif event<300 & response==4294934528 %incorrectly responded nonword to word stim;2000
                newevent=event+2000;
            end 
            
            
            new_events(eventnum,1:3)=og_events(eventnum,1:3);
            new_events(eventnum,4)=newevent;
            new_events(eventnum+1,:)=og_events(eventnum+1,:);
            
            behav_events(eventnum,1)=newevent;
        end %if a trigger is directly followed by a response
   end %for each event
   
   %remove NaN values (removed events) from event list
   new_events=new_events(~any(isnan(new_events),2),:);
   behav_events=behav_events(~any(isnan(behav_events),2),:);
   % create new event file in 'events' location with '_grouped' indicating
   %  completed processing
   filename=sprintf('/autofs/space/clive_001/users/olivia/GE_new/MEG/GE_%s/events/GE_%s_B%d_grouped.eve',participant_num,participant_num,block);
   fid = fopen(filename, 'w'); %'w' is what allows it to create a new file
   
   %enter new events into 'grouped' eventfile 
   for i_event = 1:size(new_events, 1)
       fprintf(fid, '%6d%- 8.3f %6d %5d\n',...
           new_events(i_event, 1), new_events(i_event, 2), new_events(i_event, 3), new_events(i_event, 4));
   end % For Each event
   
   fclose(fid);
   
    if plot_behav=='Y'
        if block==1
            allblock_events=behav_events(:,1);
        else
            allblock_events=vertcat(allblock_events,behav_events(:,1));
        end %if block 1, create allblock_events. else, just add other block's events
        
    end %if plotting behav data=yes
   
   
   
   
end %for each block

%Extract behavioral results and plot as stacked bar graph
    %note: coded all as separate objects for max flexibility for future
    %behavioral result analyses
if plot_behav=='Y'
    %'Word' Stimulus
    word=allblock_events(find(allblock_events<3000));
    word_correctresp=allblock_events(find(allblock_events<2000)); %responded correctly
    word_incorrectresp=allblock_events(find(allblock_events<3000 & allblock_events>2000)); %responded incorrectly
    
    %number of total events for good stim, good+yes response, good+no response
    N_word=length(word)
    N_word_correctresp=length(word_correctresp)
    N_word_incorrectresp=length(word_incorrectresp)
    

    
    %'NonWord' stimulus
    nonword=allblock_events(find(allblock_events>3000));
    nonword_correctresp=allblock_events(find(allblock_events<4000 & allblock_events>3000));%responded correctly
    nonword_incorrectresp=allblock_events(find(allblock_events>4000));
     %number of total events for good stim, good+yes response, good+no response
    N_nonword=length(nonword)
    N_nonword_correctresp=length(nonword_correctresp)
    N_nonword_incorrectresp=length(nonword_incorrectresp)
    
    overall_percent_accuracy=(100*(N_word_correctresp+N_nonword_correctresp))/(N_word+N_nonword)
    %plot as stacked bar graph
    figure(1)
    bar([N_word_correctresp N_word_incorrectresp; N_nonword_correctresp N_nonword_incorrectresp],'stacked');
    legend('correct response','incorrect response');
    title(sprintf(' word (1) vs. non-word (2) performance for GE%s \n %0.2f percent accuracy overall',participant_num,overall_percent_accuracy));
    xlabel('stimulus');
    ylabel('number of trials');
    
    %option to save figs as jpg once you've seen the image
    savefigs=input('Do you want to save the figure as a JPG?(Y or N)');
        %  savefigs='N';
        %  savefigs='Y';
    if savefigs=='Y'
        %format/resize fig to fill screen for continuity
        fignum=1;
        figSize = [58, 48];            % [width, height]
        figUnits = 'Centimeters';
        set(fignum, 'Units', figUnits);
        pos = get(fignum, 'Position');
        pos = [pos(1)+figSize(1), pos(4)+figSize(2), pos(3)+figSize(1), pos(4)+figSize(2)];
        set(fignum, 'Position', pos); %fill screen
        print_fignum=sprintf('-f%d',fignum);
        fig_save_name=sprintf('/autofs/space/clive_001/users/olivia/GE_new/MEG/GE_%s/behavioralresults_GE%s',participant_num,participant_num); %name of jpg
        print(print_fignum,fig_save_name,'-djpeg'); %save jpg
        
    end %if you want the figure jpgs stored
end %if plotting behav data='Y' ie yes

